<?php

include_once 'equipement.class.php';
include_once 'except/positionStore.class.php';

class Store extends Equipement
{
	protected $position;

	public function __construct($m_ID, $m_position, $m_IDElement, $m_description, $m_nomPiece, $m_droit, $m_droitUtilisateur)
	{
		parent::__construct($m_ID, $m_IDElement, $m_description, $m_nomPiece, $m_droit, $m_droitUtilisateur);

		$this->position = $m_position;		
	}

	public function __set($nom, $valeur)
	{
		if($nom == "position")
		{
			$this->position = $valeur;
		}
	}

	public function __get($name)
	{
		if(property_exists(get_class(), $name))
		{
			return $this->$name;
		}
	}

	public function afficheEquipement()
	{
		echo "<div class='store'>
				<p>Store : " .$this->description. "</p>";
				if($this->droit < $this->droitUtilisateur)
				{
					echo "<p class='noDroit'>Vous n'avez le droit ne modifier cet équipement</p>";
				}
				echo "<div class='position'>
				<label for='storePosition" .$this->ID. "'>Position : </label><input type='number' name='storePosition" .$this->ID. "' max='100' min='0' value='" .$this->position. "' ";
				if($this->droit < $this->droitUtilisateur)
				{
					echo "readonly='readonly'";
				}
					echo "/>
						</div>
						</div>";
	}
	
	public function afficheModifierEquipement()
	{
		echo "<input type='checkbox' name='suppStore" .$this->ID. "' value='true'>Supprimer le store : " .$this->description;
	}

	public function majEquipement()
	{
		if(isset($_POST['storePosition' .$this->ID]) && $_POST['storePosition' .$this->ID] !=  $this->position)
		{
			if($_POST['storePosition' .$this->ID] > 100)
			{
				throw new PositionStore("Position maximale : 100% pour la piece : " .$this->nomPiece);
			}
			else if($_POST['storePosition' .$this->ID] < 0)
			{
				throw new PositionStore("Position minimale : 0% pour la piece : " .$this->nomPiece);
			}
			else
			{
				try 
				{
					$maj = SPDO::pdo()->prepare("INSERT INTO Stores(Position) VALUES(:pos) WHERE ID_Store = :id");
					$maj->execute(array('pos' => $this->position,
							'id' => $this->ID));
				}
				catch(PDOException $e)
				{
					die($e->getMessage());
				}
				
			}
		}
	}
	
	public static function ajoutEquipement($m_description, $m_IDPiece, $m_droit)
	{
		try
		{
			$insert = SPDO::pdo()->prepare("INSERT INTO Element(ID_Piece, Description, Droit) VALUES (:piece, :desc, :droit)");
			$insert->execute(array('piece' => $m_IDPiece, 
					'desc' => $m_description,
					'droit'  => $m_droit));
				
			$IDElement = SPDO::pdo()->lastInsertId();
				
			$insert = SPDO::pdo()->prepare("INSERT INTO Stores(ID_Element) VALUES (:id)");
			$insert->execute(array('id' =>  $IDElement));
		}
		catch (PDOException $e)
		{
			die($e->getMessage());
		}
	}
	
	public function supprimerEquipement()
	{
		if(isset($_POST['suppStore' .$this->ID]))
		{
			try
			{
				$delete = SPDO::pdo()->prepare("DELETE FROM Stores WHERE ID_Store = :id");
				$delete->execute(array('id' => $this->ID));
			}
			catch (PDOException $e)
			{
				die($e->getMessage());
			}
			
			unset($_POST['suppStore' .$this->ID]);
		}
	}
}
